package selef.xiafan.dao;

import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import selef.xiafan.entity.Customer;

import java.util.List;

public interface CustomerDao extends JpaRepository<Customer, Integer> {
    List<Customer> findAll();

    List<Customer> findByName(String customerName);

    List<Customer> findAllByPhone(String phone);

    void deleteById(Integer customerId);

    @Query(value = "SELECT COUNT(*) FROM t_customer", nativeQuery = true)
    Long countNoQuery();

    @Query(value = "SELECT COUNT(*) FROM t_customer ct WHERE ct.car_num LIKE CONCAT('%',:carNum,'%') and ct.name LIKE CONCAT('%',:name,'%') ", nativeQuery = true)
    Long countWithQuery(@Param("name") String name, @Param("car_num") String carNum);

    @Query(value = "SELECT * FROM t_customer WHERE name LIKE CONCAT('%',:customerName,'%')", nativeQuery = true)
    List<Customer> findAllByQuery(@Param("name") String customerName);

    @Query(value = "SELECT * FROM t_customer LIMIT :page,:size", nativeQuery = true)
    List<Customer> findAllNoQuery(@Param("page") Integer page, @Param("size") Integer size);
}
